package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags={"部门接口"})
@RestController //@Controller和@ResponseBody的组合注解
@RequestMapping("/dept") //暴露公共的路径
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;


    /**
     * 分页接口
     * 1.请求方式 2.请求路径 3.分析参数接收 4.返回值(total:总条数 list:当前页数据)
     * @return
     */
    @PostMapping("/findPage")
    @ApiOperation(value="查询分页", notes="查询部门分页列表")
    public AjaxResult findPage(@RequestBody DepartmentQuery query){

        //调用service层分页方法
        return AjaxResult.createSuccess(departmentService.findPage(query));
    }


    @GetMapping("/findAll")
    @ApiOperation(value="查询部门", notes="查询部门列表")
    public AjaxResult findAll(){
        //1.查询
        List<Department> departments = departmentService.findAll();
        //2.返回数据
        return AjaxResult.createSuccess(departments);
    }

    @GetMapping("/findParentDepts")
    @ApiOperation(value="查询上级部门", notes="查询上级部门列表")
    public AjaxResult 查询上级部门(){
        //1.查询
        List<Department> departments = departmentService.findParentDepts();
        //2.返回数据
        return AjaxResult.createSuccess(departments);
    }

    /**
     * 获取部门树
     * @return
     */
    @GetMapping("/treeDept")
    @ApiOperation(value="获取部门树", notes="获取部门树")
    public AjaxResult treeDept(){
        //1.查询
        List<Department> departments = departmentService.treeDept();
        //2.返回数据
        return AjaxResult.createSuccess(departments);
    }


    /**
     * 新增/修改
     * @return
     */
    @PostMapping
    @ApiOperation(value="新增或者修改", notes="没有id就是新增，有id就是修改")
    public AjaxResult saveOrUpdate(@Valid @ApiParam(value = "新增部门参数", required = true)
                                       @RequestBody Department department){

        try {
            //新增/修改
            departmentService.saveOrUpdate(department);
            return AjaxResult.createSuccess();
        }catch (RuntimeException e) {
            //打印错误的堆栈信息
            e.printStackTrace();
            return AjaxResult.createError(e.getMessage());
        } catch (Exception e) {
            //打印错误的堆栈信息
            e.printStackTrace();
            return AjaxResult.createError("操作异常，联系管理员李裕");
        }
    }

    /**
     *删除
     * @return
     * /dept/11
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value="删除一个部门", notes="就是删除")
    public AjaxResult delete(@ApiParam(value = "主键id", required = true) @PathVariable("id") Long id){

        try {
            //新增/修改
            departmentService.delete(id);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError("操作异常，联系管理员李裕");
        }
    }

    /**
     * 批量删除
     * @return
     * /dept/11
     */
    @PostMapping("/batchDelete")
    @ApiOperation(value="删除一个部门", notes="就是删除")
    public AjaxResult batchDelete(@RequestBody DepartmentQuery query){

        try {
            //新增/修改
            departmentService.batchDelete(query);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError("操作异常，联系管理员李裕");
        }
    }

}
